'''
Author: lyc
Date: 2020-11-24 15:19:19
LastEditors: lyc
LastEditTime: 2021-01-12 16:03:11
Description: 登录
            第一版 是将登录app作为整个otree的一部分，将用户名和密码当作数据收集
            第二版 是将登录模块用url单独实现，该页所有的class都没有用到，因为不把login放到settings.py的app队列里面，url里面就找不到对应的函数

'''
from otree.api import Currency as c, currency_range
from ._builtin import Page, WaitPage
from .models import Constants
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from django.shortcuts import render,redirect
import settings
import os
import csv


#TODO 提交表单 数据检查
@csrf_exempt
def checkLogin(request):
    usrname = request.POST.get('usrname')
    password = request.POST.get('password')
    slug = request.POST.get('slug')
    play_id = request.POST.get('play_id')
    url_pre = request.POST.get('url_pre')


    # 读取 指定路径下文件的数据
    with open(os.path.join(settings.PROJECT_DIR, 'oTree/collect_static', 'user.csv'), 'r', encoding="gbk") as f:
        reader = csv.reader(f)
        reader_row = next(reader)
        # 记录第几次循环
        num = 0
        print("账号密码为:"+usrname+"和"+password)
        print("playID为:"+play_id)
        # //第几个玩家对应登录的第几个号 如P3就是u3+u3来登录
        # row代表每一列
        for row in reader:
            num = num + 1
            if num == int(play_id) and row[0].split()[0] == usrname and password == row[0].split()[1]:
                print('------------V 用户名密码存在 v--------------')
                print(num,row[0].split()[0],row[0].split()[1])
                request.session.set_expiry(0)
                # 登录成功 存session
                #重要步骤 如果做机器人可以把这里改成多个token 再进行之后的计算
                request.session['token'] = play_id
                break;
            else:
                print('------------v 用户名密码不存在 v--------------')
                print(num,row[0].split()[0], row[0].split()[1])



    # print(usrname,password,'-',slug,'-',play_id)
    # print('{}/InitializeParticipant/{}'.format(url_pre,slug))
    # return redirect('{}/InitializeParticipant/{}'.format(url_pre,slug))
    return redirect(url_pre)


def my_view(request):
    return HttpResponse('恭喜 你发现了一个小彩蛋')


class MyPage(Page):
    form_model = 'player'
    form_fields = ['userName', 'passWord']

    def is_displayed(self):
        return False

    def app_after_this_page(self, upcoming_apps):
        userName = self.player.userName
        passWord = self.player.passWord
        # 获取当前角色对应的id
        player_number = settings.SESSION_CONFIGS[0]['num_demo_participants']
        if self.player.id%player_number == 0:
            id = player_number
        else :
            id = self.player.id%player_number

        # 读取 指定路径下文件的数据
        with open(os.path.join(settings.PROJECT_DIR, 'oTree/collect_static', 'user.csv'), 'r', encoding="gbk") as f:
            reader = csv.reader(f)
            reader_row = next(reader)
            # 记录第几次循环
            num = 0
            # row代表每一列
            for row in reader:
                num = num+1
                if num == id and row[0].split()[0] == userName and passWord == row[0].split()[1]:
                    return upcoming_apps[0]
                else:
                    pass

    def vars_for_template(self):
        return dict(
            round = self.round_number
        )


page_sequence = [MyPage]
